package com.stylefeng.guns.modular.apple.utils;


import com.stylefeng.guns.core.common.exception.BizExceptionEnum;
import com.stylefeng.guns.core.exception.GunsException;
import com.stylefeng.guns.core.exception.GunsExceptionEnum;
import com.stylefeng.guns.modular.upload.FileInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * 上传文件到本地，不考虑数据资源分离的情况
 */
@Slf4j
public class LocalFileUtils {
    /**
     * 文件上传方法
     * @param file
     * @param fileInfo
     */
    public static void uploadFile(MultipartFile file, FileInfo fileInfo){
        log.info("上传文文件开始 ========>");
        if (file == null){
            log.error("上传文文件====> 文件未找到！" + fileInfo.getFileName());
            throw new GunsException(BizExceptionEnum.FILE_NOT_FOUND);
        }

        try {
            File tmp = new File(fileInfo.getTmpPath());
            if (!tmp.exists()){
                tmp.mkdirs();
            }
            file.transferTo(new File(fileInfo.getTmpPath()+"/"+fileInfo.getFileName()));
        } catch (IOException e) {
            log.error("上传文文件====> 数据写入报错,{}",e.getMessage());
            throw new GunsException(GunsExceptionEnum.FILE_NOT_FOUND);
        }

        log.info("上传文文件结束 ========>");
    }
}
